package com.fancy.learncenter.common;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;

import com.fancy.learncenter.utils.LogUtil;
import com.shuyu.waveview.*;

import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by hyy on 2017/12/11.
 * describe as
 */

public class MAudioPlayer implements MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener {
    private MediaPlayer mMediaPlayer;

    public MAudioPlayer() {
        try {
            this.mMediaPlayer = new MediaPlayer();
            this.mMediaPlayer.setAudioStreamType(3);
            this.mMediaPlayer.setOnBufferingUpdateListener(this);
            this.mMediaPlayer.setOnPreparedListener(this);
            this.mMediaPlayer.setOnCompletionListener(this);
            this.mMediaPlayer.setOnErrorListener(this);
        } catch (Exception var4) {
            var4.printStackTrace();

            LogUtil.MyLog("MAudioPlayer", "====var4====");
        }
    }

    public MAudioPlayer(Context context, int res) {
        try {
            this.mMediaPlayer = MediaPlayer.create(context, res);
            this.mMediaPlayer.setAudioStreamType(3);
            this.mMediaPlayer.setOnBufferingUpdateListener(this);
            this.mMediaPlayer.setOnPreparedListener(this);
            this.mMediaPlayer.setOnCompletionListener(this);
            this.mMediaPlayer.setOnErrorListener(this);
        } catch (Exception var4) {
            var4.printStackTrace();

        }
    }

    public void playUrl(String url) {
        try {
            this.mMediaPlayer.reset();
            this.mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            this.mMediaPlayer.setDataSource(url);
            this.mMediaPlayer.prepareAsync();
            LogUtil.MyLog("MAudioPlayer", "====playUrl====");
        } catch (IllegalArgumentException var3) {
            var3.printStackTrace();

            LogUtil.MyLog("MAudioPlayer", "====var3====");
        } catch (SecurityException var4) {
            var4.printStackTrace();
            LogUtil.MyLog("MAudioPlayer", "====var4====");
        } catch (IllegalStateException var5) {
            LogUtil.MyLog("MAudioPlayer", "====var5====");
            var5.printStackTrace();
        } catch (IOException var6) {
            var6.printStackTrace();
            LogUtil.MyLog("MAudioPlayer", "====var6====");
        }
    }

    public boolean isPlaying() {
        if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
            return true;
        }
        return false;
    }

    /**
     * 播放器，到某个时间段-开始播放
     *
     * @param time
     */
    public void seekTo(int time) {
        if (this.mMediaPlayer != null) {
            this.mMediaPlayer.seekTo(time);
            this.mMediaPlayer.start();
        }

    }

    /**
     * 播放器，播放暂停
     */
    public void setLooping(boolean looping) {
        if (this.mMediaPlayer != null) {
            this.mMediaPlayer.setLooping(looping);
        }
    }

    /**
     * 播放器，播放暂停
     */
    public void pause() {
        if (audioPlayerStatus != null) {
            audioPlayerStatus.playerPause();
        }

        if (this.mMediaPlayer != null) {
            this.mMediaPlayer.pause();
        }
    }


    /**
     * 播放器，开始播放
     */
    public void play() {
        if (audioPlayerStatus != null) {
            audioPlayerStatus.playerStart();
        }
        this.mMediaPlayer.start();
    }

    /**
     * 播放器，播放停止
     */
    public void stop() {
        if (audioPlayerStatus != null) {
            audioPlayerStatus.playerPause();
        }
        if (this.mMediaPlayer != null) {
            this.mMediaPlayer.stop();
            this.mMediaPlayer.release();
            this.mMediaPlayer = null;
        }
    }

    /**
     * 播放器，准备完成
     *
     * @param mp
     */
    public void onPrepared(MediaPlayer mp) {
        if (audioPlayerStatus != null) {
            audioPlayerStatus.playerPrepared(mp);
        }
    }

    /**
     * 播放器，播放完成
     *
     * @param mp
     */
    public void onCompletion(MediaPlayer mp) {
        if (audioPlayerStatus != null) {
            audioPlayerStatus.playerCompletion(mp);
        }
    }


    public void onBufferingUpdate(MediaPlayer mp, int percent) {
    }

    /**
     * 播放器，播放出现错误
     *
     * @param mp
     */
    public boolean onError(MediaPlayer mp, int what, int extra) {
        if (audioPlayerStatus != null) {
            audioPlayerStatus.playerError(mp, what, extra);
        }
        return false;
    }

    public long getDuration() {
        return (long) this.mMediaPlayer.getDuration();
    }

    public static long getDurationLocation(Context context, String path) {
        MediaPlayer player = MediaPlayer.create(context, Uri.fromFile(new File(path)));
        return player != null ? (long) player.getDuration() : 0L;
    }


    public void setAudioPlayerStatus(AudioPlayerStatus audioPlayerStatus) {
        this.audioPlayerStatus = audioPlayerStatus;
    }

    private AudioPlayerStatus audioPlayerStatus;

    /**
     * 播放器播放状态
     */
    public interface AudioPlayerStatus {
        abstract void playerPause();

        abstract void playerCompletion(MediaPlayer mp);

        abstract void playerPrepared(MediaPlayer mp);

        abstract void playerStart();

        abstract void playerError(MediaPlayer mp, int what, int extra);
    }
}
